# RESTful DDS is a prototyping effort to demonstrate how DDS could be combined
# with HTTP clients, extending the realm of pub/sub distribution to true wide
# area networking.
#
# Copyright (C) 2009 PrismTech Ltd.
# reinier.torenbeek@gmail.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License Version 3 dated 29 June 2007, as published by the
# Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with restful-dds; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

GEN_DIR=generated
OBJ_DIR=obj
LIB_DIR=lib
SRC_DIR=src

SERVICE_GEN_DIR=$GEN_DIR
SERVICE_OBJ_DIR=$OBJ_DIR
SERVICE_LIB_DIR=$LIB_DIR

SERVICE_PKG=org.opensplice.restful.service
SERVICE_DIR=org/opensplice/restful/service
SERVICE_SOURCE_DIR=$SRC_DIR/$SERVICE_DIR
SERVICE_JAR=$SERVICE_LIB_DIR/$SERVICE_PKG.jar

CHAT_MODULE=Chat
CHAT_SOURCE_DIR=$SERVICE_GEN_DIR/$CHAT_MODULE

# Source shell script containing locations of external jar files.
# Edit externals.sh if you want to point to the jars other than the
# default locations.
source ./externals.sh
EXTERNAL_JARS=$RESTLET_INTF_JAR:$RESTLET_IMPL_JAR:$GSON_JAR

# First generate code for built in test
# This is not required for the service to work, but is left in here for
# testing purposes.
echo Generating test classes using OpenSplice
mkdir -p $SERVICE_GEN_DIR
idlpp -S -l java -d $SERVICE_GEN_DIR $CHAT_IDL

# Compile generated source code
echo Compiling generated Tutorial source code
mkdir -p $SERVICE_OBJ_DIR
javac -sourcepath $SERVICE_GEN_DIR -d $SERVICE_OBJ_DIR -classpath "$SERVICE_OBJ_DIR:$DDS_JAR" $CHAT_SOURCE_DIR/*.java

# Compile service source code
echo Compiling RESTfulDDS service source code
mkdir -p $SERVICE_OBJ_DIR
javac -sourcepath $SERVICE_SOURCE_DIR -d $SERVICE_OBJ_DIR -classpath "$SERVICE_OBJ_DIR:$DDS_JAR:$EXTERNAL_JARS" $SERVICE_SOURCE_DIR/*.java

# Create jar
echo Creating RESTfulDDS service jar file, including the Chat files
mkdir -p $SERVICE_LIB_DIR
jar cf $SERVICE_JAR -C $SERVICE_OBJ_DIR $SERVICE_DIR  -C $SERVICE_OBJ_DIR $CHAT_MODULE

# Prepare to build java chatroom example
if [ -e Tutorial ]
then
    echo Skipped building of chatroom tutorial because it seems to be there already
else
    echo Copying and building OpenSplice chatroom tutorial
    cp -r $TUTORIAL_DIR .
    (cd Tutorial
    sh BUILD)
fi


# Build the Battleship demo
BATTLESHIP_NAME=battleship
BATTLESHIP_DIR=demos/$BATTLESHIP_NAME
BATTLESHIP_OBJ_DIR=$BATTLESHIP_DIR/$OBJ_DIR
BATTLESHIP_LIB_DIR=$BATTLESHIP_DIR/$LIB_DIR
BATTLESHIP_SOURCE_DIR=$BATTLESHIP_DIR/$SRC_DIR
BATTLESHIP_JAR=$BATTLESHIP_LIB_DIR/$BATTLESHIP_NAME.jar

TRACK_IDL=$BATTLESHIP_DIR/idl/track.idl
TRACK_MODULE=track
TRACK_GEN_DIR=$BATTLESHIP_DIR/$GEN_DIR
TRACK_SOURCE_DIR=$TRACK_GEN_DIR/$TRACK_MODULE
TRACK_OBJ_DIR=$BATTLESHIP_OBJ_DIR/$GEN_DIR
TRACK_JAR=$BATTLESHIP_LIB_DIR/$TRACK_MODULE.jar

echo Generating typed classes for Battleship demo
mkdir -p $TRACK_GEN_DIR
idlpp -S -l java -d $TRACK_GEN_DIR "$TRACK_IDL"

echo Compiling generated Battleship code
mkdir -p $TRACK_OBJ_DIR
javac -sourcepath $TRACK_GEN_DIR -d $TRACK_OBJ_DIR -classpath $TRACK_OBJ_DIR:"$DDS_JAR" $TRACK_SOURCE_DIR/*.java

echo Compiling Battleship source files
BATTLESHIP_PACKAGES="track Battlefield Display Identification Sensor"
for f in $BATTLESHIP_PACKAGES
do
  javac -d $BATTLESHIP_OBJ_DIR -classpath $BATTLESHIP_OBJ_DIR:$TRACK_OBJ_DIR:"$DDS_JAR" $BATTLESHIP_SOURCE_DIR/$f/*.java
done

echo Creating Battleship jar file
mkdir -p $BATTLESHIP_LIB_DIR
JAR_PARAMS=
for f in $BATTLESHIP_PACKAGES
do
  JAR_PARAMS="$JAR_PARAMS -C $BATTLESHIP_OBJ_DIR $f"
done
jar cf $BATTLESHIP_JAR $JAR_PARAMS

echo Creating track jar file
jar cf $TRACK_JAR -C $TRACK_OBJ_DIR $TRACK_MODULE


# Only compile web application if the GWT_HOME environment variable has been
# set. Otherwise, skip this step.
if [ -z "$GWT_HOME" ]
then
  echo Environment variable GWT_HOME not set, skipping web display compilation
  exit
fi

WEBAPP_PKG=DisplayWebApp
WEBAPP_ENTRY_POINT=display.$WEBAPP_PKG
WEBAPP_SUBDIR=display/client
WEBAPP_DIR=$BATTLESHIP_SOURCE_DIR/$WEBAPP_PKG
WEBAPP_SOURCE_DIR=$WEBAPP_DIR/$SRC_DIR
WEBAPP_DISPLAY_SOURCE_DIR=$WEBAPP_SOURCE_DIR/$WEBAPP_SUBDIR
WEBAPP_OBJ_DIR=$BATTLESHIP_OBJ_DIR
WEBAPP_JAR=$BATTLESHIP_LIB_DIR/webdisplay.jar
WEBAPP_WWW_DIR=$BATTLESHIP_DIR/www
GWT_USER_JAR=$GWT_HOME/gwt-user.jar
#GWT_MAPS_JAR=external/gwt-maps.jar
GWT_MAPS_JAR=
GWT_DEV_JAR=$GWT_HOME/gwt-dev.jar

echo Compiling Battlefield web display java source
mkdir -p $WEBAPP_OBJ_DIR
javac -sourcepath $WEBAPP_SOURCE_DIR -d $WEBAPP_OBJ_DIR -classpath "$TRACK_OBJ_DIR:$GWT_USER_JAR:$GWT_MAPS_JAR" $WEBAPP_DISPLAY_SOURCE_DIR/*.java

echo Creating Battlefield web display jar
jar cf $WEBAPP_JAR -C $WEBAPP_OBJ_DIR $WEBAPP_SUBDIR

echo GWT-compiling Java source code into java script
mkdir -p $WEBAPP_WWW_DIR
java -Xmx256M -classpath "$WEBAPP_SOURCE_DIR:$WEBAPP_JAR:$GWT_USER_JAR:$GWT_MAPS_JAR:$GWT_DEV_JAR" com.google.gwt.dev.Compiler $WEBAPP_ENTRY_POINT -war $WEBAPP_WWW_DIR
#java -Xmx256M -classpath "$WEBAPP_SOURCE_DIR:$WEBAPP_JAR:$GWT_USER_JAR:$GWT_MAPS_JAR:$GWT_DEV_JAR" com.google.gwt.dev.DevMode $WEBAPP_ENTRY_POINT -war $WEBAPP_WWW_DIR
